//
//  CRBCumtomAddView.m
//  ChampsARotate
//
//  Created by 吴凡 on 2018/12/28.
//  Copyright © 2018 Logan.Fan. All rights reserved.
//

#import "CRBCumtomAddView.h"

@implementation CRBCumtomAddView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    __weak __typeof(self)weakSelf = self;
    self.backgroundColor = [UIColor clearColor];
    
    _tipLabel = [UILabel new];
    _tipLabel.text = @"*未找到相关xx，如确认无误，可选择已输入的内容";
    _tipLabel.textColor = [UIColor colorWithHex:@"333333"];
    _tipLabel.textAlignment = NSTextAlignmentLeft;
    _tipLabel.font = [UIFont fontWithName:@"PingFang SC" size:12];
    [self addSubview:_tipLabel];
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self.mas_left).offset(15);
    }];
    
    _bgView = [UIView new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tipLabel.mas_bottom).offset(7);
        make.left.equalTo(weakSelf.tipLabel);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(50);
    }];
    _bgView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    [_bgView addGestureRecognizer:tap];
    
    _contentLabel = [UILabel new];
    _contentLabel.text = @"";
    _contentLabel.textColor = [UIColor colorWithHex:@"333333"];
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    _contentLabel.font = [UIFont fontWithName:@"PingFang SC" size:15];
    [_bgView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.bgView);
        make.left.equalTo(weakSelf.bgView.mas_left).offset(16);
        make.right.equalTo(weakSelf.bgView.mas_right).offset(-16);
    }];
  
}

- (void)drawRect:(CGRect)rect {
    [self viewAddRounderCornerWithRadius:8 borderWidth:2 borderColor:[UIColor RGBColorWithR:51 g:51 b:51] view:_bgView];
}

- (void)viewAddRounderCornerWithRadius:(CGFloat)radius borderWidth:(CGFloat)width borderColor:(UIColor *)color view:(UIView*)view {
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, view.mj_w, view.mj_h) cornerRadius:radius];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    
    if (radius != 0) {
        maskLayer.path = bezierPath.CGPath;
    }
    maskLayer.frame = CGRectMake(0, 0, view.mj_w, view.mj_h);
    
    if (width != 0 && color) {
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = CGRectMake(0, 0, view.mj_w, view.mj_h);
        borderLayer.lineWidth = width;
        borderLayer.strokeColor = color.CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        borderLayer.path = bezierPath.CGPath;
        [view.layer insertSublayer:borderLayer atIndex:0];
    }
    [view.layer setMask:maskLayer];
}



- (void)tapClick:(UITapGestureRecognizer *)tap {
    if ([_contentLabel.text isEqualToString:@""] || _contentLabel.text.length == 0) {
        return;
    }
    self.contentLabel.textColor =  UIColor.whiteColor;
    self.bgView.backgroundColor = [UIColor RGBColorWithR:51 g:51 b:51];
    
    if (_selectedFinishedBlock) {
        _selectedFinishedBlock(_contentLabel.text);
    }
}
@end
